var ControlChatEmbed = function(){
};

ControlChatEmbed.prototype = {
	run : function(req, res){
		this.user = new (require("../models/m_user"));
		this.req = req;
		this.res = res;
		this.data = {};
		this.thisUser = {};
		var _this = this;

		if(this.req.body.login == 'Chat'){
			// Người dùng đăng nhập để chat
			this.data.username = this.req.body.username;
			this.data.username = this.req.body.username;
			var _this = this;
			this.Authorization(this.req.body.username, this.req.body.password, function(err, doc){
				if(err || !doc){
					_this.data.showLogin = true;
				}else{
					// console.log(doc);
					_this.data.showLogin = false;
					// Lưu cookie
					_this.res.cookie('username', doc.username, { domain: '.schat.vn',/* secure: true,*/ path: '/', httpOnly: true, signed: true});
					_this.res.cookie('password', doc.password, { domain: '.schat.vn',/* secure: true,*/ path: '/', httpOnly: true, signed: true});
					_this.res.cookie('id', doc.idChat, { domain: '.schat.vn',/* secure: true,*/ path: '/', httpOnly: true, signed: true});

					// Đưa thông tin ng dùng vào biến
					// _this.thisUser.username = doc.username;
					// _this.thisUser.password = doc.password;
					// _this.thisUser.id = doc.idChat;
				}
				_this.RenderInterface();
			});
		}else if(this.req.body.save == 'Save'){
			// Người dùng đăng ký tk
			this.user.Register(this.req.body.username, this.req.body.password,
							function(state, err, doc){
								if(state){
									// Lưu cookie
									_this.SaveCookie(_this.res, {username : doc.username, password : doc.password, id : doc.idChat});

									// Đưa thông tin ng dùng vào biến
									// _this.thisUser.username = doc.username;
									// _this.thisUser.password = doc.password;
									// _this.thisUser.id = doc.idChat;
								}else{
									_this.data.showLogin = true;
								}
								// console.log('state Register: '+state);
								_this.RenderInterface();
							});
		}else{
			// Khi f5 trình duyệt kt cookie có tồn tại hay không
			if(this.req.signedCookies.username && this.req.signedCookies.password){
				this.AuthorizationNoEncrypt(this.req.signedCookies.username, this.req.signedCookies.password,
									function(err, doc){
										// console.log(err, doc);
										if(err || !doc){
											_this.data.showLogin = true;
										}
										_this.RenderInterface();
									});
			}else{
				this.data.showLogin = true;
				this.RenderInterface();
			}
		}
	},

	Authorization : function(username, password, callback){
		this.user.Authorization(username, password, callback);
	},

	AuthorizationNoEncrypt : function(username, password, callback){
		this.user.AuthorizationNoEncrypt(username, password, callback);
	},

	Register : function(username, password, callback){
		this.user.Register(username, password, callback);
	},

	RenderInterface : function(){
		this.res.render('v_chatEmbed.ect', this.data);
	},

	SaveCookie : function(res, obj){
		for(i in obj){
			res.cookie(i, obj[i], { domain: '.schat.vn',/* secure: true,*/ path: '/', httpOnly: true, signed: true});
		}
	}
};

module.exports = new ControlChatEmbed();
/*
module.exports = function(req, res) {
	var data = {username : ''},
		user = new (require("../models/m_user"));

	if(req.body.login == 'Chat'){
		data.username = req.body.username;
		user.Authorization(req.body.username, req.body.password, function(err, doc){
			if(doc !== undefined){
				data.showLogin = false;
				res.cookie('username', doc.username, { /*domain: '.example.com', secure: true,* / path: '/', httpOnly: true, signed: true});
				res.cookie('password', doc.password, { /*domain: '.localhost', secure: true,* / path: '/', httpOnly: true, signed: true});
				res.cookie('id', doc.idChat, { /*domain: '.localhost', secure: true,* / path: '/', httpOnly: true, signed: true});
			}else{
				data.showLogin = true;
			}
			renderInterface(res, data);
		});
	}else if(req.body.save == 'Save'){
		user.Register(req.body.username, req.body.password);
		renderInterface(res, data);
	}else{
		data.showLogin = true;
		renderInterface(res, data);
	}

	function renderInterface(res, data){
		res.render('v_chatEmbed.ect', data);
	}
}*/